unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Spin, StdCtrls,
  ExtCtrls, windows;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    CheckBox1: TCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    SpinEdit1: TSpinEdit;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private

  public
    procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY;

  end;

var
  Form1: TForm1;
  HotKeyId: Integer;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Interval := SpinEdit1.Value;
  Timer1.Enabled := True;
  Button2.Enabled := True;
  Button1.Enabled := False;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Timer1.Enabled := False;
  Button2.Enabled := False;
  Button1.Enabled := True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  HotKeyId := GlobalAddAtom('WJDR');
  RegisterHotKey(Handle, HotKeyId, MOD_ALT, VK_F1);
  RegisterHotKey(Handle, HotKeyId, MOD_ALT, VK_F2);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotKey(handle, HotKeyId);
  GlobalDeleteAtom(HotKeyId);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  RandVar, Range: Integer;
begin
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  Sleep(100);
  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

  Randomize;
  Range := SpinEdit1.Value div 10;
  RandVar := Random(Range * 2);
  Timer1.Interval := SpinEdit1.Value - Range + RandVar;
end;

procedure TForm1.HotKeyDown(var Msg: Tmessage);
begin
  if (Msg.LparamLo = MOD_ALT) AND (Msg.LParamHi = VK_F1) then // ALT+F1
  begin
    if Button1.Enabled then
      Button1.Click;
  end;
  if (Msg.LparamLo = MOD_ALT) AND (Msg.LParamHi = VK_F2) then // ALT+F2
  begin
    if Button2.Enabled Then
      Button2.Click;
  end;
end;

end.

